package com.spark.colliery

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.support.v7.app.AppCompatActivity
import com.blankj.utilcode.util.SPUtils
import com.spark.common.Const
import com.spark.colliery.module.mine.activity.LoginActivity

class SplashActivity : AppCompatActivity() {
    private val toMain = 0x11
    private val toReg = 0x12

    companion object {
        fun actionStart(host: Activity) {
            val intent = Intent(host, SplashActivity::class.java)
            host.startActivity(intent)
        }
    }

    private val handler: Handler = Handler(Looper.getMainLooper()) {
        when (it.what) {
            toMain -> {
                MainActivity.actionStart(this)
                finish()
            }
            toReg -> {
                LoginActivity.actionStart(this)
                finish()
            }
        }

        return@Handler true
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_splash)
        val initInstall = SPUtils.getInstance().getBoolean("init_install", true)
        if (initInstall) {
            GuideActivity.actionStartBack(this)
        } else {
            loginCheck()
        }
    }

    override fun onResume() {
        super.onResume()
    }

    private fun loginCheck() {
        handler.sendEmptyMessageDelayed(if (Const.isLogined()) toMain else toReg, 1500)
    }
}